import { axiosMock, delayFn, randArrItem } from '@/services/axiosMock'
import { DEFAULT_PAGE_SIZE } from '@/utils/pagination'
import { ITemplate, ITemplateRes } from '../interface'

const htmlBody = `<h1>Welcome,Client {{Client_Name}}!</h1>
<p>Thanks for use Order ID {{ Order_ID }}. We’re thrilled to have you on board. To get the most out of {{ Shipping_Method }}, do this primary next step:</p>
<!-- Action -->
<table class="body-action" align="center" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center"></td>
     <td class="attributes_item"><strong>Estimated Delivery Date:</strong> {{Estimated_Delivery_Date}}</td>
     <td class="attributes_item"><strong>Destination:</strong> {{Destination}}</td>
  </tr>
</table>
<p>For reference, here's your login information:</p>
<table class="attributes" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td class="attributes_content">
      <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
          <td class="attributes_item"><strong>FBA ID:</strong> {{FBA_ID}}</td>
        </tr>
        <tr>
          <td class="attributes_item"><strong>Estimated Delivery Window:</strong> {{Estimated_Delivery_Window}}</td>
        </tr>
      </table>
    </td>
  </tr>
</table>
<p>You've started a {{route_time}} day trial. You can upgrade to a paying account or cancel any time.</p>
<table class="attributes" width="100%" cellpadding="0" cellspacing="0">
  <tr>
    <td class="attributes_content">
      <table width="100%" cellpadding="0" cellspacing="0">
        <tr>
          <td class="attributes_item"><strong>Carton Quantity:</strong> {{Carton_Quantity}}</td>
        </tr>
         <tr>
          <td class="attributes_item"><strong>Route Time:</strong> {{Route_Time}}</td>
        </tr>
        <tr>
          <td class="attributes_item"><strong>Routing Description:</strong> {{Routing_Description}}</td>
        </tr>
      </table>
    </td>
  </tr>
</table>`

const createTemplate = (id: string | number, tplRequired?: boolean): ITemplate => ({
  templateId: `${id}`,
  title: randArrItem(['模板标题模板标题模板标题模板标题模板标题'], 0.1),
  type: randArrItem(['模板类型'], 0.1),
  subject: randArrItem(['Welcome to {{Routing_Title}}'], tplRequired ? undefined : 0.1),
  htmlBody: randArrItem([htmlBody], tplRequired ? undefined : 0.1),
})

axiosMock('default', mock => {
  // 获取消息
  mock.onPost(`/tmsapi/message_manage/list`).reply(
    delayFn(600, ({ data }) => {
      return {
        success: true,
        data: {
          total: 2500,
          records: Array(+data.size || DEFAULT_PAGE_SIZE)
            .fill(null)
            .map((_item, index) => {
              const count = index + 1
              return {
                messageId: `${data.page}${count}`,
                templateId: `${data.page}${count}`,
                enableFlag: randArrItem([0, 1]),
                channelType: randArrItem(['1', '2', '3', '4']),
                eventType: randArrItem([1, 2]),
                receiveObj: randArrItem([1, 2]),
                routeIds: [1, 2],
                sendType: randArrItem([1, 2, 3]),
                title: `测试消息标题${count}`,
                type: randArrItem([1, 2, 3]),
                updateAt: randArrItem([`2024-11-16 12:12:12`], 0.1),
              }
            }),
        },
      }
    }),
  )

  // 新增消息
  mock.onPost(`/tmsapi/message_manage/add`).reply(
    delayFn(600, () => {
      return { success: true }
    }),
  )

  // 删除消息
  mock.onPost(`/tmsapi/message_manage/del/.+`).reply(
    delayFn(600, () => {
      return { success: true }
    }),
  )

  // 详情
  mock.onGet(RegExp(`/tmsapi/message_manage/detail/.+`)).reply(
    delayFn(600, () => {
      return {
        success: true,
        data: {
          messageId: `123`,
          templateId: `1233`,
          enableFlag: 1,
          channelType: 1,
          eventType: 1,
          receiveObj: 1,
          routeIds: [1, 2],
          sendType: 1,
          title: `测试消息标题`,
          type: 1,
          updateAt: randArrItem([`2024-11-16 12:12:12`], 0.1),
          messageRouteList: [],
          templateInfo: {
            templateId: '1233',
            title: '模板名称',
            type: '发布路由',
            imageUrl: 'www.baidu.com',
            createAt: '111',
          },
        },
      }
    }),
  )

  // 邮件模板列表
  mock.onPost('/tmsapi/template/list').reply(
    delayFn<ITemplateRes>(600, ({ data }) => {
      const idPrefix = `${Date.now()}`
      return {
        success: true,
        data: {
          total: 2500,
          records: Array(+data.size || DEFAULT_PAGE_SIZE)
            .fill(null)
            .map((_item, index) => createTemplate(`${idPrefix}_${index}`)),
        },
      }
    }),
  )

  // 邮件模板
  mock.onPost('/tmsapi/template/getTemplate').reply(
    delayFn<ITemplate | null>(600, ({ data }) => ({
      success: true,
      data: createTemplate(data.templateId),
    })),
  )
})
